﻿using AutoMapper;
using MediatR;
using SheepStorage.API.Application.Command.SheepPurchaseCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.Dto.InputDto;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Service.Interface;

namespace SheepStorage.API.Application.CommandHandlers.SheepPurchaseHandler
{
    public class SheepPurchaseSelectHandler:IRequestHandler<SheepPurchaseSelectCommand,Results<PageOutput<SheepPurchase>>>
    {
        private readonly ISheepPurchaseService _purchaseService;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchaseService"></param>
        /// <param name="mapper"></param>
        public SheepPurchaseSelectHandler(ISheepPurchaseService purchaseService,IMapper mapper)
        {
            _purchaseService = purchaseService;
            _mapper = mapper;
        }

        /// <summary>
        /// 羊只采购列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Results<PageOutput<SheepPurchase>>> Handle(SheepPurchaseSelectCommand request, CancellationToken cancellationToken)
        {
            var purchaseList=_mapper.Map<SheepPurchaseInput>(request);

            var list = _mapper.Map<PageOutput<SheepPurchase>>(await _purchaseService.GetSheepPurchase(purchaseList));

            return Results<PageOutput<SheepPurchase>>.DataResult(list);
        }
    }
}
